package com.cat.dataStructure02;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/word-subsets/description/
 * @create 2025/10/21 20:02
 * @since JDK17
 */

public class Solution39 {
    public List<String> wordSubsets(String[] words1, String[] words2) {
        List<String> ans = new ArrayList<>();
        int[] h = new int[26], t = new int[26];
        for (var word : words2) {
            Arrays.fill(t, 0);
            for (var c : word.toCharArray()) {
                t[c - 'a']++;
            }
            for (int i = 0; i < 26; i++) {
                h[i] = Math.max(h[i], t[i]);
            }
        }
        int n = words1.length;
        for (int i = 0; i < n; i++) {
            var word = words1[i].toCharArray();
            Arrays.fill(t, 0);
            for (var c : word) {
                t[c - 'a']++;
            }
            boolean flag = true;
            for (int j = 0; j < 26; j++) {
                if (t[j] < h[j]) {
                    flag = false;
                }
            }
            if (flag) {
                ans.add(words1[i]);
            }
        }

        return ans;
    }
}